package com.xvaca.spcms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.log4j.Logger;

import com.xvaca.spcms.bean.UserBean;
import com.xvaca.spcms.db.DBConnector;

public class UserDAO {

	private static Logger logger = Logger.getLogger(UserDAO.class);
	private static final String GET_USER_BY_NAME_AND_PWD = "SELECT * FROM users WHERE username = ? AND password = ? AND status = 1";
	
	public UserBean getUserByUsernameAndPassword(String username, String password) {
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		UserBean user = null;
		
		try {
			
			conn = DBConnector.getConnection();
			pstmt = conn.prepareStatement(GET_USER_BY_NAME_AND_PWD);
			pstmt.setString(1, username);
			pstmt.setString(2, password);
			
			rs = pstmt.executeQuery();
			
			if (rs.next()) {
				user = new UserBean();
				user.setUsername(username);
				user.setPassword(password);
			}
			
		} catch (Exception e) {
			logger.error("根据用户名和密码查询用户失败", e);
		}
		
		return user;
		
	}
	
}
